package com.senac.faces;


import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import com.senac.model.Aluno;
import com.senac.service.AlunoServiceLocal;


@ManagedBean(name="alunoMBean")
@RequestScoped
public class AlunoMBean {

	@EJB
	private AlunoServiceLocal service;

	private Aluno aluno = new Aluno();
	private List<Aluno> list;
	private Integer alunoId;
	
	public String doSave() {
		try {
			service.save(aluno);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "alunos";
	}
	
	public Aluno getAluno() {
		return aluno;
	}

	public void setAluno(Aluno aluno) {
		this.aluno = aluno;
	}

	public AlunoServiceLocal getService() {
		return service;
	}

	public void setService(AlunoServiceLocal service) {
		this.service = service;
	}
	
	public List<Aluno> getList() {
		if(list == null) {
			refreshList();
		}
		return list;
	}

	public void setList(List<Aluno> list) {
		this.list = list;
	}
	
	public Integer getAlunoId() {
		return alunoId;
	}

	public void setAlunoId(Integer alunoId) {
		this.alunoId = alunoId;
	}
	
	public String editar() {
		aluno = service.getById(alunoId);
		return "aluno";
	}
	
	public String remover() {
		service.delete(alunoId);
		refreshList();
		return "alunos";
	}
	
	public String salvar() {
		try {
			service.save(aluno);	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "alunos";
	}
	
	private void refreshList() {
		list = service.get();
	}
	
}
